//
//  TestButton.m
//  POP_User
//
//  Created by Rowling on 2019/10/24.
//  Copyright © 2019 Rowling. All rights reserved.
//

#import "TestButton.h"
//#import "TimerTool.h"
#import "TestNetTool.h"
//#import <ReactiveObjC.h>
#import "UIView+SGExtension.h"

#define SG_SCREEN_WIDTH      ([UIScreen mainScreen].bounds.size.width)
#define SG_SCREEN_HEIGHT     ([UIScreen mainScreen].bounds.size.height)

@interface TestButton()
@property(nonatomic,strong)UIPanGestureRecognizer * pan;
@property(nonatomic,assign)NSInteger  time;
@property(nonatomic,strong)CAEmitterLayer * la;


//@property(nonatomic,strong)UILabel * label;
@end

@implementation TestButton


-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

-(void)setUI {
//    self.label = [UILabel new];
//    self.label.text = @"网络\n日志";
//    self.label.font = [UIFont systemFontOfSize:14];
//    self.label.numberOfLines = 0;
//    [self.label sizeToFit];
//    [self addSubview:self.label];
//    self.label.center = CGPointMake(self.sgWidth / 2, self.sgHeight / 2);
    [self addPan];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self addGestureRecognizer:tap];
    self.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"position_m"].CGImage);
//    [[TimerTool shareInstance] addDelegete:self];
    
//    self.la = [CAEmitterLayer new];
//    self.la.emitterPosition = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
//    self.la.preservesDepth = YES;
//    CAEmitterCell * cell = [CAEmitterCell new];
//    cell.birthRate = 20;
//    cell.lifetime = 5;
//    cell.lifetimeRange = 1.5;
//    cell.scale = 0.7;
//    cell.scaleRange = 0.2;
//    cell.emissionLongitude = M_PI_2;
//    cell.emissionRange = M_PI_2 / 2;
//    cell.velocity = 150;
//    cell.velocityRange = 100;
//    cell.spin = M_PI_2;
//    cell.contents = (__bridge id _Nullable)([UIImage imageNamed:@"tree_add"].CGImage);
//    self.la.emitterCells = @[cell];
//    [self.layer addSublayer:self.la];
    
//    CAAnimationGroup * g = [CAAnimationGroup new];
//    CABasicAnimation * a = [CABasicAnimation new];
//    a.keyPath = @"transform.scale";
//    a.toValue = @1.2;
//    a.fromValue = @1;
//    a.duration = 2;

    
//    CABasicAnimation * b = [CABasicAnimation new];
//    b.keyPath = @"transform.scale";
//    b.toValue = @1;
//    b.fromValue = @1.2;
//    b.duration = 2;
    
//    let sacle = CABasicAnimation.init(keyPath: "transform.scale")
//    sacle.toValue = NSNumber(value: 1)
//    sacle.fromValue = NSNumber(value: 0.2)
//    g.duration = 4;
//    g.animations = @[a,b];
//    g.repeatCount = MAXFLOAT;
//    g.removedOnCompletion = NO;
//    let keyFrame = CAKeyframeAnimation.init(keyPath: "transform.scale")
//    keyFrame.values = [NSNumber(value: 1), NSNumber(value: 1.3), NSNumber(value: 0.8), NSNumber(value: 1)]
//    keyFrame.duration = 0.3
//    keyFrame.isRemovedOnCompletion = false
//    keyFrame.delegate = self
    CAKeyframeAnimation * a = [CAKeyframeAnimation new];
    a.keyPath = @"transform.scale";
    a.values = @[@1, @1.3, @1, @1, @1, @1, @1, @1, @1, @1];
    a.duration = 10;
    a.repeatCount = MAXFLOAT;
    a.removedOnCompletion = NO;
    [self.layer addAnimation:a forKey:@"what"];
}

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
}

//-(void)timerGo {
//    self.time ++;
//    if (self.time > 2) {
//        self.alpha = 0.3;
//    }
//}


-(void)addPan {
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panMove:)];
    self.pan = pan;
    [self addGestureRecognizer:pan];
}

- (void)layoutSubviews {
    [super layoutSubviews];
}


-(void)panMove:(UIPanGestureRecognizer *)pan {
    CGPoint  point = [pan translationInView:self];
    self.center = CGPointMake((point.x ) + self.center.x, (point.y) + self.center.y);
    [pan setTranslation:CGPointZero inView:self];
    self.alpha = 1;
    self.time = 0;
    if (pan.state == UIGestureRecognizerStateEnded) {
        if (self.frame.origin.x < 0 ) {
            [UIView animateWithDuration:0.25 animations:^{
                self.sgX = 0;
            }];
        }
        if (self.frame.origin.x > SG_SCREEN_WIDTH - self.sgWidth ) {
                   [UIView animateWithDuration:0.25 animations:^{
                       self.sgX = SG_SCREEN_WIDTH - self.sgWidth;
                   }];
        }
        if (self.frame.origin.y < 0) {
            [UIView animateWithDuration:0.25 animations:^{
                self.sgY = 0;
            }];
        }
        if (self.frame.origin.y > SG_SCREEN_HEIGHT - self.sgHeight) {
           [UIView animateWithDuration:0.25 animations:^{
               self.sgY = SG_SCREEN_HEIGHT - self.sgHeight;
           }];
        }
        

    }
    
    if (pan.state == UIGestureRecognizerStateChanged) {
        NSLog(@"changed");
        UIView * v = [[FireView alloc] initWithFrame:CGRectMake(self.center.x, self.center.y, 8, 8)];
        v.layer.cornerRadius = 4;
        v.backgroundColor = [UIColor yellowColor];
        [[UIApplication sharedApplication].keyWindow insertSubview:v belowSubview:self];
    }
}

-(void)tap {
    [[UIApplication sharedApplication].keyWindow addSubview:[TestNetTool shareInstance].testView];
    [[TestNetTool shareInstance].testView reloadData];
}


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

@end


@implementation FireView

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];
    [self doAnimation];
}

-(void)doAnimation {
    [UIView animateWithDuration:1.5 animations:^{
        CGRect f = self.frame;
        f.origin.y += 50;
        CGFloat r = arc4random() % 100 - 50;
        f.origin.x += r;
        self.frame = f;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
//    [self what];
}

-(void)what {
   
}

-(void)dealloc {
    NSLog(@"FireView dealloc");
}

@end
